const { verifyToken } = require("../utils/jwt");

function authMiddleWare(req, res, next) {
  if (!req.headers.authorization) {
    res.send({
      code: 40001,
      message: "no Login",
    });
    return;
  } else {
    let token = req.headers.authorization.split(" ")[1];
    // iat: 签发时间  exp: 过期时间 id: 创建时传递的参数
    const decoded = verifyToken(token);
    if (decoded.code === 40001) {
      res.send({
        code: 40001,
        message: "登录已过期~",
      });
      return;
    }
    console.log(decoded.id, "==============16");
    req.auth = decoded.id;
    next();
  }
}

module.exports = authMiddleWare;
